package br.com.fatesg.www.controle;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.fatesg.www.dao.DAO;
import br.com.fatesg.www.dao.IndustriaDAO;
import br.com.fatesg.www.entidade.Industria;
import br.com.fatesg.www.util.ExcecaoInfraEstrutura;
import br.com.fatesg.www.util.HibernateUtil;

public class IndustriaSV extends HttpServlet {

	private static final long serialVersionUID = 2884080681637266096L;

	DAO<Industria, Long> dao = new IndustriaDAO(HibernateUtil.getSessionFactory().openSession());

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String acao = request.getParameter("acao");

			Industria industria;

			industria = new Industria();
			Long cnpj = (request.getParameter("txtCnpj") == "" || request.getParameter("txtCnpj") == null) ? 0 : Long.parseLong(request
					.getParameter("txtCnpj"));
			if (cnpj > 0)
				industria.setCnpj(cnpj);

			industria.setNome(request.getParameter("txtRazaoSocial"));
			industria.setEmail(request.getParameter("txtEmail"));
			industria.setCnpj(cnpj);
			industria.setEndereco(request.getParameter("txtEndereco"));
			industria.setTelefone(request.getParameter("txtTelefone"));

			if (acao.equals("novo")) {
				request.setAttribute("industria", new Industria());
				request.getRequestDispatcher("frmIndustria.jsp").forward(request, response);
			}
			if (acao.equals("editar")) {
				Industria industriaObtida = dao.obterPeloId(Industria.class, cnpj);
				request.setAttribute("industria", industriaObtida);
				request.getRequestDispatcher("frmIndustria.jsp").forward(request, response);
			}
			if (acao.equals("inserir")) {
				Industria industriaObtida = dao.obter(cnpj);

				if (industriaObtida != null && industriaObtida.getCnpj() != null) {
					industriaObtida.setCnpj(industria.getCnpj());
					industriaObtida.setNome(industria.getNome());
					industriaObtida.setTelefone(industria.getTelefone());
					industriaObtida.setEndereco(industria.getEndereco());
					industriaObtida.setEmail(industria.getEmail());

					dao.atualizar(industriaObtida);
				} else {
					dao.salvar(industria);
				}

				request.setAttribute("lstIndustria", dao.listar(Industria.class));
				request.getRequestDispatcher("lstIndustria.jsp").forward(request, response);
			} else if (acao.equals("atualizar")) {
				dao.atualizar(industria);
				request.setAttribute("lstIndustria", dao.listar(Industria.class));
				request.getRequestDispatcher("lstIndustria.jsp").forward(request, response);
			} else if (acao.equals("excluir")) {

				dao.excluir(dao.obterPeloId(Industria.class, cnpj));
				request.setAttribute("lstIndustria", dao.listar(Industria.class));
				request.getRequestDispatcher("lstIndustria.jsp").forward(request, response);
			} else if (acao.equals("obter")) {
				dao.obter(cnpj);

			} else if (acao.equals("listar")) {
				request.setAttribute("lstIndustria", dao.listar(Industria.class));
				request.getRequestDispatcher("lstIndustria.jsp").forward(request, response);
			}
		} catch (Exception ex) {
			dao.getSession().getTransaction().rollback();
			dao.getSession().close();
			Throwable tx = ex.getCause();
			request.setAttribute("msg", "Execaoo: " + ex.getMessage());
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}

	}

}
